/*
 * 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.idock.frame.download;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.util.Log;
import cn.idock.frame.PzApplication;

/**
 * Single remote file download task
 * 
 * @author idock
 */
public class DownloadJob {

	private String mDestination;

	private DownloadTask mDownloadTask;
	private DownloadJobListener mListener;

	private int mProgress;
	private int mTotalSize;
	private int mDownloadedSize;

	private int mStartId;

	private DownloadManager mDownloadManager;

	private String mUrl;

	public String getUrl() {
		return mUrl;
	}

	public void setUrl(String url) {
		this.mUrl = url;
	}

	public String getDestination() {
		return mDestination;
	}

	public void setDestination(String destination) {
		mDestination = destination;
	}

	public DownloadJob(String url, String destination, int startId) {
		mUrl = url;
		mDestination = destination;
		mProgress = 0;
		mStartId = startId;
		mDownloadManager = PzApplication.getInstance().getDownloadManager();
	}

	@SuppressLint("NewApi")
	public void start() {
		mDownloadTask = new DownloadTask(this);
//		mDownloadTask.execute();
		mDownloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	public void pause() {
		// TODO DownloadTask.pause()
	}

	public void resume() {
		// TODO DownloadTask.resume()
	}

	public void cancel() {
		mDownloadTask.cancel(true);
	}

	public void setListener(DownloadJobListener listener) {
		mListener = listener;
	}

	public int getProgress() {
		return mProgress;
	}

	public void setProgress(int progress) {
		mProgress = progress;
	}

	public void setTotalSize(int totalSize) {
		this.mTotalSize = totalSize;
	}

	public int getTotalSize() {
		return mTotalSize;
	}

	public void setDownloadedSize(int downloadedSize) {
		this.mDownloadedSize = downloadedSize;
		int oldProgress = mProgress;
		mProgress = (mDownloadedSize * 100) / mTotalSize;
		if (mProgress != oldProgress) {
			mDownloadManager.notifyObservers();
		}
	}

	public int getDownloadedSize() {
		return mDownloadedSize;
	}

	public void notifyDownloadStarted() {
		if (mListener != null)
			mListener.downloadStarted();
		mProgress = 0;
	}

	public void notifyDownloadEnded() {
		if (!mDownloadTask.isCancelled()) {
			if (mListener != null)
				mListener.downloadEnded(this);
			mProgress = 100;
		}
	}

	public void setStartId(int mStartId) {
		this.mStartId = mStartId;
	}

	public int getStartId() {
		return mStartId;
	}
}
